package com.glodon.paas.account.web.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.glodon.paas.account.dao.domain.UserAdviceEntity;
import com.glodon.paas.account.dao.domain.UserAdviceReplyEntity;
import com.glodon.paas.account.security.spring.LoginUser;
import com.glodon.paas.account.service.UserAdviceService;
import com.glodon.paas.account.web.util.SecurityUtils;
import com.glodon.paas.consts.StringConst;

@Controller
@RequestMapping("/")
public class UserAdviceController{ 
	
	@Autowired
	private UserAdviceService userAdviceService;
	
	 
	
	public static final Logger LOGGER = LoggerFactory.getLogger(UserAdviceController.class);
	
	@RequestMapping(value = "/addAdvice", method = RequestMethod.POST)
	public void addUser(
			@RequestParam(value="type", required=false) Integer type,
			@RequestParam(value="userName", required=false) String userName,
			@RequestParam(value="title", required=false) String title,
			@RequestParam(value="content", required=true) String content, 			 
			HttpServletRequest request, 
			HttpServletResponse response,
			ModelMap model) {
		
		UserAdviceEntity advice = new UserAdviceEntity();
		LoginUser user = SecurityUtils.getCurrentUser();
		advice.setUserId(user.getId());
		if(StringUtils.isNotBlank(userName)){
			advice.setUserName(userName);
		}else{
			advice.setUserName(user.getDisplayName());
		}
		
		advice.setType(type);
		advice.setTitle(title);
		advice.setContent(content);
		
		userAdviceService.createAdvice(advice);
		
		//model.addAttribute(StringConst.ID, advice.getId());
		model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
		//return new ModelAndView("userinfo");
	}
	
	@RequestMapping(value = "/getAdvice", method = RequestMethod.POST)
	public void getAdvice(	
			@RequestParam(value="adviceId", required=true) String adviceId,		 
			HttpServletRequest request, 
			HttpServletResponse response,
			ModelMap model) {
		UserAdviceEntity record = userAdviceService.getAdviceById(adviceId);		 
		model.addAttribute(StringConst.RESULT, record);
	}
	
	@RequestMapping(value = "/queryAdvice", method = RequestMethod.POST)
	public void queryAdvice(
			@RequestParam(value="status", required=false) Integer status, 
			@RequestParam(value="type", required=false) Integer type,
			@RequestParam(value="deleteStatus", required=false, defaultValue="0") Integer deleteStatus, 	
			@RequestParam(value="startIndex", required=false,defaultValue="0") Integer startIndex, 	
			@RequestParam(value="count", required=false,defaultValue="100") Integer count,
			HttpServletRequest request, 
			HttpServletResponse response,
			ModelMap model) {
		LoginUser user = SecurityUtils.getCurrentUser();
		
		List<UserAdviceEntity> list = userAdviceService.queryAdvice(user.getId(), status, type, deleteStatus, startIndex, count);
		
		model.addAttribute(StringConst.RESULT, list);
	}
	
	@RequestMapping(value = "/replyAdvice", method = RequestMethod.POST)
	public void replyAdvice(
			@RequestParam(value="adviceId", required=true) String adviceId,
			@RequestParam(value="content", required=true) String content, 	
			@RequestParam(value="replyUserType", required=true) Integer replyUserType,
			HttpServletRequest request, 
			HttpServletResponse response,
			ModelMap model) {
		LoginUser user = SecurityUtils.getCurrentUser();
		
		UserAdviceReplyEntity entity = new UserAdviceReplyEntity();
		entity.setReplyUserId(user.getId());		
		entity.setReplyUserName(user.getDisplayName());
		
		entity.setAdviceId(adviceId);
		entity.setContent(content);
		entity.setReplyUserType(replyUserType);
		
		userAdviceService.createReply(entity);
		
		model.addAttribute(StringConst.ID, entity.getId());
		model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
	}

	@RequestMapping(value = "/getAdviceReply", method = RequestMethod.POST)
	public void getReply(
			@RequestParam(value="adviceId", required=true) String adviceId,
			@RequestParam(value="startIndex", required=false,defaultValue="0") Integer startIndex, 	
			@RequestParam(value="count", required=false,defaultValue="100") Integer count,
			HttpServletRequest request, 
			HttpServletResponse response,
			ModelMap model) {		
		
		List<UserAdviceReplyEntity> list = userAdviceService.queryReply(adviceId, startIndex, count);
		
		model.addAttribute(StringConst.RESULT, list);
	}
}
